package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.App;
import flc.ast.R;
import flc.ast.activity.CumulativePasswordActivity;
import flc.ast.activity.PasswordActivity;
import flc.ast.adapter.PasswordFunctionAdapter;
import flc.ast.bean.MyPassKindBean;
import flc.ast.databinding.FragmentPasswordBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PasswordFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PasswordFragment extends BaseNoModelFragment<FragmentPasswordBinding> {

    private PasswordFunctionAdapter functionAdapter;
    private List<MyPassKindBean> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_password;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.llPasswordCount.setOnClickListener(this);
        mDataBinding.flWifiPassword.setOnClickListener(this);
        mDataBinding.flAppPassword.setOnClickListener(this);

        mDataBinding.rvFunctionList.setLayoutManager(new GridLayoutManager(mContext, 4));
        functionAdapter = new PasswordFunctionAdapter();
        mDataBinding.rvFunctionList.setAdapter(functionAdapter);
        functionAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llPasswordCount://累计存储密码
                startActivity(CumulativePasswordActivity.class);
                break;
            case R.id.flWifiPassword://wifi密码
                List<MyPassKindBean> listSp = AppSpUtils.getPasswordList();
                PasswordActivity.kindBean = listSp.get(0);
                startActivity(PasswordActivity.class);
                break;
            case R.id.flAppPassword://app密码
                List<MyPassKindBean> listSp1 = AppSpUtils.getPasswordList();
                PasswordActivity.kindBean = listSp1.get(1);
                startActivity(PasswordActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PasswordActivity.kindBean = listShow.get(position);
        startActivity(PasswordActivity.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    //获取记录
    private void getData() {
        listShow.clear();
        int countPass = SPUtil.getInt(mContext, Content.countPass, 0);//获取存放总条数
        mDataBinding.tvPasswordCount.setText(countPass + "");

        String useTime = SPUtil.getString(mContext, Content.useTime, "");//获取使用时间
        if (TextUtils.isEmpty(useTime)) {//第一次使用
            SPUtil.putString(mContext, Content.useTime, TimeUtils.getNowString());
            mDataBinding.tvProtectionDuration.setText(1 + "");
            //创建记录
            List<MyPassKindBean> list = new ArrayList<>();
            list.add(new MyPassKindBean(R.drawable.wifimima, R.drawable.wifimm1, getString(R.string.password_type1), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.appmima, R.drawable.app1, getString(R.string.password_type2), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.zh, R.drawable.zh1, getString(R.string.password_type3), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.yx, R.drawable.yx1, getString(R.string.password_type4), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.qb, R.drawable.qb1, getString(R.string.password_type5), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.kh, R.drawable.kh1, getString(R.string.password_type6), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.yxi, R.drawable.yx12, getString(R.string.password_type7), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.wz, R.drawable.wz1, getString(R.string.password_type8), new ArrayList<>()));
            list.add(new MyPassKindBean(R.drawable.qt, R.drawable.qt1, getString(R.string.password_type9), new ArrayList<>()));
            //保存记录
            AppSpUtils.savePasswordList(list);
            listShow.addAll(list.subList(2, list.size()));
            mDataBinding.tvWifiPasswordCount.setText(getString(R.string.record_count, 0));
            mDataBinding.tvAppPasswordCount.setText(getString(R.string.record_count, 0));
        } else {
            long day = TimeUtils.getTimeSpanByNow(useTime, TimeConstants.DAY);
            day = Math.abs(day);
            if (day == 0) {
                day++;
            }
            mDataBinding.tvProtectionDuration.setText(day + "");
            List<MyPassKindBean> listSp = AppSpUtils.getPasswordList();
            if (listSp != null && listSp.size() > 0) {
                listShow.addAll(listSp.subList(2, listSp.size()));
            }
            mDataBinding.tvWifiPasswordCount.setText(getString(R.string.record_count, listSp.get(0).getList().size()));
            mDataBinding.tvAppPasswordCount.setText(getString(R.string.record_count, listSp.get(1).getList().size()));
        }
        functionAdapter.setList(listShow);
    }

}